const weiUtils = require('../../utils/util')
const config = require('../../utils/config')
import moment from "moment";
Page({
  data: {
    active: 0,
    userinfo:{},
    lunarData:{},
    bzData:{},
    todayData:{},
    dyData:{},
    heavenlyStems :['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'],
    earthlyBranches : ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'],
    qian:'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/八卦_乾.svg',
    kun:'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/八卦_坤.svg'
  },
  onLoad(options) {
    //this.initData();
//判断 stroge 中 是否存在 unserinfo 不存在 则跳转 home/index 存在赋值 userinfo
    const userinfo = wx.getStorageSync('userinfo');
    if(userinfo){
      this.setData({ userinfo });
      
      let that = this;
      weiUtils.request({
        url: `https://zhangteng.cloud/5001/api/Rss/GetDate?dete=${that.data.userinfo.year}-${that.data.userinfo.month}-${that.data.userinfo.day}%20${that.data.userinfo.hour}:${that.data.userinfo.minute}:00`,
        success(data) {
        console.log('data2:', data)
          
        that.setData({
          lunarData: data
          });
         console.log(`request获取的数据2: `, that.data.lunarData)
        }
      });
      weiUtils.request({
        url: `https://bzapi2.iwzbz.com/getbasebz3.php?d=${that.data.userinfo.year}-${that.data.userinfo.month}-${that.data.userinfo.day}%20${that.data.userinfo.hour}:${that.data.userinfo.minute}:00&s=1&today=${moment().format("YYYY-MM-DD HH:mm")}&vip=0&userguid=&yzs=1`,
        success(data) {
        console.log('data:', data)
        data.dy = [];
        let obj ={};
        obj.name = '小运';
        obj.year = that.data.userinfo.year
        data.dy.push(obj);
        data.dayun.forEach((element,i) => {
          let obj ={};
         
          if (i===0)
          {
            obj.name = element;
            obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0])
          }
          else{
            obj.name = element;
            obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0]) + ((i)*10)
          }
          data.dy.push(obj);
          
        });
        data.cgcolor = [];
        data.cg.forEach((element) => {
          obj ={}
          obj.name = element;
          obj.color = []
          element.forEach((item) => {
            obj.color.push(that.getClassByElement(item))
          });
          data.cgcolor.push(obj)
         
        });
        console.log('data.cgcolor:', data.cgcolor)
        let dy = that.findNameByYear(moment().year(),data.dy)
        console.log('dy:', dy)
      data.dyyear = that.findNameByYear2(moment().year(),data.dy)
       data.daYunT = dy.split('')[0];
       data.daYunTcolor = that.getClassByElement(data.daYunT)
       //data.cgYunT = that.getCg(data.daYunT)
       data.daYunZ = dy.split('')[1];
       data.daYunZcolor = that.getClassByElement(data.daYunZ)
       data.cgYunZ= that.getCg(data.daYunZ)
       data.cgYunZcolor=[];
       data.cgYunZ.forEach(element => {
         data.cgYunZcolor.push(that.getClassByElement(element))
       });
       console.log('data.cgYunZ:', data.cgYunZ)
       data.bzcolor = [];
       
      
        data.bzcolor.push(that.getClassByElement(data.bz[0]))
        data.bzcolor.push(that.getClassByElement(data.bz[1]))
        data.bzcolor.push(that.getClassByElement(data.bz[2]))
        data.bzcolor.push(that.getClassByElement(data.bz[3]))
        data.bzcolor.push(that.getClassByElement(data.bz[4]))
        data.bzcolor.push(that.getClassByElement(data.bz[5]))
        data.bzcolor.push(that.getClassByElement(data.bz[6]))
        data.bzcolor.push(that.getClassByElement(data.bz[7]))
     
        console.log('data:', data)
        that.setData({
          bzData: data
          });

          weiUtils.request({
            url: `https://bzapi2.iwzbz.com/getbasebz3.php?d=${that.data.bzData.dyyear}-${that.data.userinfo.month}-${that.data.userinfo.day}%20${that.data.userinfo.hour}:${that.data.userinfo.minute}:00&s=1&today=${moment().format("YYYY-MM-DD HH:mm")}&vip=0&userguid=&yzs=1`,
            success(data) {
            console.log('data:', data)
            data.dy = [];
            let obj ={};
            obj.name = '小运';
            obj.year = that.data.userinfo.year
            data.dy.push(obj);
            data.dayun.forEach((element,i) => {
              let obj ={};
             
              if (i===0)
              {
                obj.name = element;
                obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0])
              }
              else{
                obj.name = element;
                obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0]) + ((i)*10)
              }
              data.dy.push(obj);
              
            });
            data.cgcolor = [];
            data.cg.forEach((element) => {
              obj ={}
              obj.name = element;
              obj.color = []
              element.forEach((item) => {
                obj.color.push(that.getClassByElement(item))
              });
              data.cgcolor.push(obj)
             
            });
            console.log('data.cgcolor:', data.cgcolor)
            let dy = that.findNameByYear(moment().year(),data.dy)
            console.log('dy:', dy)
           data.daYunT = dy.split('')[0];
           data.daYunTcolor = that.getClassByElement(data.daYunT)
           //data.cgYunT = that.getCg(data.daYunT)
           data.daYunZ = dy.split('')[1];
           data.daYunZcolor = that.getClassByElement(data.daYunZ)
           data.cgYunZ= that.getCg(data.daYunZ)
           data.cgYunZcolor=[];
           data.cgYunZ.forEach(element => {
             data.cgYunZcolor.push(that.getClassByElement(element))
           });
           console.log('data.cgYunZ:', data.cgYunZ)
           data.bzcolor = [];
           
          
            data.bzcolor.push(that.getClassByElement(data.bz[0]))
            data.bzcolor.push(that.getClassByElement(data.bz[1]))
            data.bzcolor.push(that.getClassByElement(data.bz[2]))
            data.bzcolor.push(that.getClassByElement(data.bz[3]))
            data.bzcolor.push(that.getClassByElement(data.bz[4]))
            data.bzcolor.push(that.getClassByElement(data.bz[5]))
            data.bzcolor.push(that.getClassByElement(data.bz[6]))
            data.bzcolor.push(that.getClassByElement(data.bz[7]))
         
            console.log('data:', data)
            that.setData({
              dyData: data
              });
              //console.log(`request获取的数据: `, this.data.bzjs_Data.base_info.name)
            }
          });
          //console.log(`request获取的数据: `, this.data.bzjs_Data.base_info.name)
        }
      });

      weiUtils.request({
        url: `https://bzapi2.iwzbz.com/getbasebz3.php?d=${moment().year()}-${that.data.userinfo.month}-${that.data.userinfo.day}%20${that.data.userinfo.hour}:${that.data.userinfo.minute}:00&s=1&today=${moment().format("YYYY-MM-DD HH:mm")}&vip=0&userguid=&yzs=1`,
        success(data) {
        console.log('data:', data)
        data.dy = [];
        let obj ={};
        obj.name = '小运';
        obj.year = that.data.userinfo.year
        data.dy.push(obj);
        data.dayun.forEach((element,i) => {
          let obj ={};
         
          if (i===0)
          {
            obj.name = element;
            obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0])
          }
          else{
            obj.name = element;
            obj.year = parseInt(that.data.userinfo.year) + parseInt(data.qiyunarr[0]) + ((i)*10)
          }
          data.dy.push(obj);
          
        });
        data.cgcolor = [];
        data.cg.forEach((element) => {
          obj ={}
          obj.name = element;
          obj.color = []
          element.forEach((item) => {
            obj.color.push(that.getClassByElement(item))
          });
          data.cgcolor.push(obj)
         
        });
        console.log('data.cgcolor:', data.cgcolor)
        let dy = that.findNameByYear(moment().year(),data.dy)
        console.log('dy:', dy)
       data.daYunT = dy.split('')[0];
       data.daYunTcolor = that.getClassByElement(data.daYunT)
       //data.cgYunT = that.getCg(data.daYunT)
       data.daYunZ = dy.split('')[1];
       data.daYunZcolor = that.getClassByElement(data.daYunZ)
       data.cgYunZ= that.getCg(data.daYunZ)
       data.cgYunZcolor=[];
       data.cgYunZ.forEach(element => {
         data.cgYunZcolor.push(that.getClassByElement(element))
       });
       console.log('data.cgYunZ:', data.cgYunZ)
       data.bzcolor = [];
       
      
        data.bzcolor.push(that.getClassByElement(data.bz[0]))
        data.bzcolor.push(that.getClassByElement(data.bz[1]))
        data.bzcolor.push(that.getClassByElement(data.bz[2]))
        data.bzcolor.push(that.getClassByElement(data.bz[3]))
        data.bzcolor.push(that.getClassByElement(data.bz[4]))
        data.bzcolor.push(that.getClassByElement(data.bz[5]))
        data.bzcolor.push(that.getClassByElement(data.bz[6]))
        data.bzcolor.push(that.getClassByElement(data.bz[7]))
     
        console.log('data:', data)
        that.setData({
          todayData: data
          });
          //console.log(`request获取的数据: `, this.data.bzjs_Data.base_info.name)
        }
      });

      
      
    }else{
      wx.navigateTo({
        url: '../home/index',
      });
    };
  },
  onReady() {

  },
  onShow() {

  },
  onHide() {

  },
  onUnload() {

  },
  onShareAppMessage() {
    return {
      title: '',
    };
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    });
  },
   getMainStar(year) {
    const heavenlyStems = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
    const mainStars = {
      '甲': '七杀星',
      '乙': '正官星',
      '丙': '偏印星',
      '丁': '正印星',
      '戊': '比肩星',
      '己': '劫财星',
      '庚': '食神星',
      '辛': '伤官星',
      '壬': '偏财星',
      '癸': '正财星'
    };
  
    // 计算天干
    const yearHeavenlyStem = heavenlyStems[(year - 4) % 10];
  
    // 获取主星
    return mainStars[yearHeavenlyStem];
  },
  findNameByYear(currentYear, data) {
  console.log('currentYear:', currentYear)
  console.log('data:', data)
    for (let i = 0; i < data.length; i++) {
      if (currentYear <= data[i].year) {
      console.log(' data[i].year:',  data[i].year)
        return data[i-1].name;
      }
    }
    return null; // 如果没有找到合适的年份，返回null
  },
  findNameByYear2(currentYear, data) {
    console.log('currentYear:', currentYear)
    console.log('data:', data)
      for (let i = 0; i < data.length; i++) {
        if (currentYear <= data[i].year) {
        console.log(' data[i].year:',  data[i].year)
          return data[i-1].year;
        }
      }
      return null; // 如果没有找到合适的年份，返回null
    },
  getClassByElement(element) {
    console.log('element:', element)
      
      // console.log(element, element.includes('金'))
      if (element.includes('庚') || element.includes('辛') || element.includes('申') || element.includes('酉')) {
        return '#f2c53f'; // 假设金色对应的背景色是黄色
      }
      if (element.includes('甲') || element.includes('乙') || element.includes('寅') || element.includes('卯')) {
        return '#228B22'; // 木色对应的背景色是绿色
      }
      if (element.includes('壬') || element.includes('癸') || element.includes('子') || element.includes('亥')) {
        return '#00BFFF'; // 水色对应的背景色是蓝色
      }
      if (element.includes('丙') || element.includes('丁') || element.includes('午') || element.includes('巳')) {
        return '#B22222'; // 火色对应的背景色是红色
      }
      if (element.includes('戊') || element.includes('己') || element.includes('辰') || element.includes('戌') || element.includes('丑') || element.includes('未')) {
        return '#A0522D'; // 土色对应的背景色是棕色
      }
      // return 'bg-gray-500'; // 默认背景色，如果没有匹配到任何一种
    },
    getCg(dz)
    {
      let arr = [];
      
      if(dz === '子')
      {
        arr = ['癸']
      }
      else if(dz === '丑')
      {
        arr = ['己', '癸', '辛']
      }
      else if(dz === '寅')
      {
         arr = ['甲', '丙', '戊']
      }
      else if(dz === '卯')
      {
         arr = ['乙']
      }
      else if(dz === '辰')
      {
         arr = ['戊', '乙', '癸']
      }
      else if(dz === '巳')
      {
         arr = ['丙', '庚','戊' ]
      }
      else if(dz === '午')
        {
           arr = ['丁', '己']
        }
        else if(dz === '未')
        {
           arr = ['己', '丁', '乙']
        }
        else if(dz === '申')
        {
           arr = ['庚', '壬', '戊']
        }
        else if(dz === '酉')
        {
           arr = ['辛']
        }
        else if(dz === '戌')
        {
          arr = ['戊', '辛', '丁']
        }
        else if(dz === '亥')
        {
         arr = ['壬', '甲']
        }
        else {
          arr = []
        }
        return arr;
    },
  initData(date) {
    if(!date)
    {
      date=moment().format("YYYY-MM-DD HH:mm");
    }
    let that = this;
    weiUtils.request({
      url: `https://zhangteng.cloud/5001/api/Rss/GetDate?dete=${date}`,
      success(data) {
      console.log('data2:', data)
        data.dao = data.dao.split('年')[0].replace('道歷','')
        data.fo = data.fo.split('年')[0]
        data.lunarDate1 = data.lunarDate.split('年')[1]
        data.lunarDate = data.lunarDate.split('年')[0]
        data.ganZhiArr = data.ganZhi.split('-')
        data.shengXiaoArr = data.shengXiao.split('-')
        data.naYinArr = data.naYin.split('-')

        data.liuNianT = data.ganZhiArr[0].split('')[0];
        data.liuNianTcolor = that.getClassByElement(data.liuNianT)
        data.liuNianZ = data.ganZhiArr[0].split('')[1];
        data.cgNianZ= that.getCg(data.liuNianZ)
        data.cgNianZcolor = []
        data.cgNianZ.forEach(element => {
          data.cgNianZcolor.push(that.getClassByElement(element))
        });
        data.liuNianZcolor = that.getClassByElement(data.liuNianZ)
        // data.naYinColorArr = [];
        // data.naYinArr.forEach(element => {
        //   data.naYinColorArr.push(that.getClassByElement(element))
        // });
      that.setData({
        todayData: data
        });
       console.log(`request获取的数据2: `, that.data.lunarData)
      }
    });
  }
});